<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="标签话题" prop="lableId">
        <el-select
          v-model="queryParams.lableId"
          placeholder="请选择标签话题"
          clearable
          filterable
          @change="handleQuery">
          <el-option
            v-for="item in labelList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
            <span :class="item.delFlag == 1 ? 'del-label' : ''">{{ item.name }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标签内容" prop="lableName">
        <el-input
          v-model="queryParams.lableName"
          placeholder="请输入标签内容"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="被举报标识" prop="reportFlag">
        <el-select v-model="queryParams.reportFlag" placeholder="请选择被举报标识" clearable>
          <el-option label="被举报" value="1"/>
        </el-select>
      </el-form-item>

      <el-form-item label="置顶状态" prop="topStatus">
        <el-select v-model="queryParams.topStatus" placeholder="请选择置顶状态" clearable>
          <el-option label="未置顶" value="0"/>
          <el-option label="置顶" value="1"/>
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="topicList">
      <el-table-column label="记录Id" align="center" prop="id" width="80"/>
      <el-table-column label="用户" align="left" width="200">
        <template slot-scope="scope">
          <div class="user-item">
            <div class="avatar-placeholder">
              <img v-if="scope.row.avatar" :src="scope.row.avatar" alt="用户头像" class="user-avatar">
              <i v-else class="el-icon-user-solid" style="font-size: 16px; color: #ccc;"></i>
            </div>
            <div class="user-info">
              <div class="username">{{ scope.row.name }}</div>
              <div class="create-time">{{ parseTime(scope.row.createTime) }}</div>
            </div>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="内容" align="left" min-width="300">
        <template slot-scope="scope">
          <div class="content-item">
            <div class="content-text" v-html="scope.row.content"></div>
            <div v-if="scope.row.imgs && scope.row.imgs.length > 0" class="image-preview">
              <image-preview
                v-for="(img, index) in scope.row.imgs.split(',')"
                :key="index"
                :src="img"
                :width="40"
                :height="40"
              />
            </div>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="标签" align="center" width="150">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.label" size="mini">{{ scope.row.label }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" width="150">
        <template slot-scope="scope">
          <div class="status-container">
            <span :class="scope.row.delFlag == 1 ? 'danger-text' : 'success-text'">
              {{ scope.row.delFlag == 1 ? '已删除' : '正常' }}
            </span>
            <span :class="scope.row.topStatus == 1 ? 'warning-text' : ''">
              {{ scope.row.topStatus == 1 ? '置顶' : '未置顶' }}
            </span>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="统计" align="center" width="200">
        <template slot-scope="scope">
          <div class="stats-container">
            <div class="stat-item">
              <i class="el-icon-thumb"></i>
              <span>{{ scope.row.greatNum || 0 }}</span>
            </div>
            <div class="stat-item">
              <i class="el-icon-chat-dot-round"></i>
              <span>{{ scope.row.replyNum || 0 }}</span>
            </div>
            <div class="stat-item">
              <i class="el-icon-warning-outline"></i>
              <span>{{ scope.row.reportNum || 0 }}</span>
            </div>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >详情
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-top"
            @click="handleSetTop(scope.row)"
            v-if="scope.row.topStatus !== 1 && scope.row.delFlag !== 1"
            v-hasPermi="['cms:topic:edit']"
          >置顶
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-bottom"
            @click="handleCancelTop(scope.row)"
            v-if="scope.row.topStatus === 1 && scope.row.delFlag !== 1"
            v-hasPermi="['cms:topic:edit']"
          >取消置顶
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.delFlag !== 1"
            v-hasPermi="['cms:topic:edit']"
          >修改
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.delFlag !== 1"
            v-hasPermi="['cms:topic:remove']"
          >删除
          </el-button>
          
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="handleAddBlacklist(scope.row)"
            v-if="scope.row.delFlag !== 1"
            v-hasPermi="['cms:topic:edit']"
          >加入黑名单
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看详情改为页面跳转 -->
    
    <!-- 加入黑名单对话框 -->
    <el-dialog title="加入黑名单" :visible.sync="openBlack" width="500px" append-to-body>
      <el-form ref="blackForm" :model="blackForm" :rules="blackRules" label-width="80px">
        <el-form-item label="禁言天数" prop="days">
          <el-select v-model="blackForm.days" placeholder="请选择禁言天数">
            <el-option label="7天" :value="7"></el-option>
            <el-option label="15天" :value="15"></el-option>
            <el-option label="30天" :value="30"></el-option>
            <el-option label="永久" :value="99999"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="禁言原因" prop="reason">
          <el-input 
            v-model="blackForm.reason" 
            type="textarea" 
            placeholder="请输入禁言原因" 
            :rows="4"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitBlackForm">确 定</el-button>
        <el-button @click="cancelBlackForm">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {delTopic, listTopic, topicSetTop} from "@/api/forum/topic"
import { listLabel } from "@/api/forum/label"
import { addBlacklist } from "@/api/forum/blackuser"
// import TopicDetail from './components/TopicDetail'

export default {
  name: "Topic",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 论坛主题表格数据
      topicList: [],
      // 标签列表
      labelList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        content: null,
        lableId: null,
        lableName: null,
        reportFlag: null,
        topStatus: null,
      },
      // 黑名单表单参数
      blackForm: {
        uid: null,
        days: 7,
        reason: ""
      },
      // 是否显示黑名单弹窗
      openBlack: false,
      // 黑名单表单验证规则
      blackRules: {
        days: [{ required: true, message: "禁言天数不能为空", trigger: "blur" }],
        reason: [{ required: true, message: "禁言原因不能为空", trigger: "blur" }]
      }
    }
  },
  created() {
    this.getList()
    this.getLabelList()
  },
  methods: {
    /** 查询论坛主题列表 */
    getList() {
      this.loading = true
      listTopic(this.queryParams).then(response => {
        this.topicList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 查询标签列表 */
    getLabelList() {
      listLabel({pageSize: 1000, delFlag: null}).then(response => {
        this.labelList = response.rows
      })
    },
    // 取消按钮
    cancel() {
    },
    // 表单重置
    reset() {
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        name: 'TopicAdd'
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        name: 'TopicEdit',
        params: {id: row.id}
      });
    },
    /** 查看详情按钮操作 */
    handleView(row) {
      this.$router.push({
        name: 'TopicDetail',
        params: {id: row.id}
      });
    },
    /** 提交按钮 */
    /** 提交按钮 */
    submitForm: function () {
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除id为"' + row.id + '"的数据项？').then(function () {
        return delTopic(row.id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
    /** 置顶操作 */
    handleSetTop(row) {
      this.$modal.confirm('是否确认置顶ID为"' + row.id + '"的数据项？').then(function () {
        return topicSetTop(row.id, 1);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("置顶成功");
      }).catch(() => {
      });
    },
    /** 取消置顶操作 */
    handleCancelTop(row) {
      this.$modal.confirm('是否确认取消置顶ID为"' + row.id + '"的数据项？').then(function () {
        return topicSetTop(row.id, 0);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消置顶成功");
      }).catch(() => {
      });
    },
    /** 加入黑名单操作 */
    handleAddBlacklist(row) {
      this.blackForm.uid = row.uid;
      this.blackForm.days = 7;
      this.blackForm.reason = "";
      this.openBlack = true;
    },
    /** 提交黑名单表单 */
    submitBlackForm() {
      this.$refs["blackForm"].validate(valid => {
        if (valid) {
          const data = {
            uid: this.blackForm.uid,
            days: this.blackForm.days,
            reason: this.blackForm.reason
          };
          addBlacklist(data).then(response => {
            this.$modal.msgSuccess("加入黑名单成功");
            this.openBlack = false;
          });
        }
      });
    },
    /** 取消黑名单表单 */
    cancelBlackForm() {
      this.openBlack = false;
      this.resetBlackForm();
    },
    /** 重置黑名单表单 */
    resetBlackForm() {
      this.blackForm = {
        uid: null,
        days: 7,
        reason: ""
      };
      this.resetForm("blackForm");
    },
  }
}
</script>

<style scoped>
.user-item {
  display: flex;
  align-items: center;
}

.avatar-placeholder {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background-color: #eaeaea;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 8px;
  flex-shrink: 0;
  overflow: hidden;
}

.user-avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-info {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.username {
  font-weight: 500;
  font-size: 14px;
  margin-bottom: 2px;
  white-space: nowrap;
}

.create-time {
  font-size: 12px;
  color: #999;
}

.stats-container {
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 8px 0;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 60px;
  padding: 4px;
}

.stat-item i {
  font-size: 18px;
  margin-bottom: 4px;
}

.stat-item .el-icon-thumb {
  color: #409EFF;
}

.stat-item .el-icon-chat-dot-round {
  color: #67C23A;
}

.stat-item .el-icon-warning-outline {
  color: #E6A23C;
}

.stat-item span {
  font-size: 14px;
  font-weight: 500;
}

.danger-text {
  color: #909399;
}

.success-text {
  color: #67C23A;
}

.warning-text {
  color: #E6A23C;
}

.del-label {
  color: #909399;
}
</style>
